<?php

namespace backend\services\system;

use backend\extensions\ApiBaseService;
use backend\models\system\DictItem;

/**
 * Desc 字典项服务类
 * @author HUI
 */
class DictItemService extends ApiBaseService {
    
    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params){
        //检索条件
        $condition = ['AND'];
        $condition[] = ['=','cate_id',$params['cate_id']];
        if($params['name']){
            $condition[] = ['like','name',$params['name']];
        }
        if($params['code']){
            $condition[] = ['=','code',$params['code']];
        }
        $list = DictItem::getDataAll($condition,$params['page'],$params['page_size']);
        return $list;
    }
    
    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function create($params){
        if(DictItem::getDataOne(['name'=>$params['name'],'cate_id'=>$params['cate_id']])){
            return self::jsonError('字典分类已存在');
        }
        if(DictItem::create($params) == false){
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }
    
    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function update($params){
        $condition = ['id'=>$params['id']];
        unset($params['id']);
        if(DictItem::updateData($params,$condition) === false){
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }
    
    /**
     * 删除
     * @param array $params 参数 
     * **/
    public static function delete($params){
        $condition = ['id'=>$params['id']];
        if(DictItem::del($condition) == false){
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }
}
